package cc.wsyw126.java.collection.list;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * Created by junpeng.yang on 16/8/26.
 */
public class TestArrayList {
    // toArray(T[] contents)调用方式一
    public Integer[] vectorToArray1(ArrayList<Integer> v) {
        Integer[] newText = new Integer[v.size()];
        v.toArray(newText);
        return newText;
    }

    // toArray(T[] contents)调用方式二。最常用！
    public Integer[] vectorToArray2(ArrayList<Integer> v) {
        Integer[] newText = (Integer[])v.toArray(new Integer[0]);
        return newText;
    }

    // toArray(T[] contents)调用方式三
    public Integer[] vectorToArray3(ArrayList<Integer> v) {
        Integer[] newText = new Integer[v.size()];
        Integer[] newStrings = (Integer[])v.toArray(newText);
        return newStrings;
    }

    public static void main(String[] args) {
        ArrayList<Integer> arrayList = new ArrayList<>(Arrays.asList(new Integer[]{1,2,4,3,5,6,8,5,4,7,3,6,7}));
        TestArrayList testArrayList = new TestArrayList();
        Integer[] integers = testArrayList.vectorToArray1(arrayList);
        System.out.println("integers.length = " + integers.length);
        System.out.println("--------------------------------");
        Integer[] integers1 =testArrayList.vectorToArray2(arrayList);
        System.out.println("integers1.length = " + integers1.length);
        System.out.println("--------------------------------");
        Integer[] integers2 = testArrayList.vectorToArray3(arrayList);
        System.out.println("integers2.length = " + integers2.length);
        System.out.println("--------------------------------");

    }
}
